/**
 * 
 */
package com.da.landlord;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.espirit.eap.sa.Privilege;

/**
 * 配置表: 权限操作类. 如: 浏览，更新
 * @author Jebel Qin
 * @date 2015年6月15日
 * @since v1.0
 */

@Entity
@Table(name="da_authaction")
public class AuthAction
{
	//权限操作ID
	private Integer id;
	
	//权限操作名称
	private String name;
	
	//归属于哪个模块 
	private AuthRoot root;
	
	private List<Privilege> privileges;
	
	//是否选中 (仅作页面显示用，不保持到DB)
	private Boolean checked;
	
	public AuthAction() {}

	public AuthAction(String name)
	{
		this.name = name;
	}
	
	public AuthAction(Integer id, String name)
	{
		super();
		this.id = id;
		this.name = name;
	}

	@Id
	@GeneratedValue
	public Integer getId()
	{
		return id;
	}

	public void setId(Integer id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	@ManyToOne
	public AuthRoot getRoot()
	{
		return root;
	}

	public void setRoot(AuthRoot root)
	{
		this.root = root;
	}

	@OneToMany(cascade=CascadeType.REFRESH)
	@JoinTable(name="da_authaction_privilege",joinColumns=@JoinColumn(name="AuthAction_id"),inverseJoinColumns = @JoinColumn(name="privileges_id"))
	public List<Privilege> getPrivileges()
	{
		return privileges;
	}

	public void setPrivileges(List<Privilege> privileges)
	{
		this.privileges = privileges;
	}

	@Transient
	public Boolean getChecked()
	{
		return checked;
	}

	public void setChecked(Boolean checked)
	{
		this.checked = checked;
	}

	@Transient
	public List<String> getPrivilegeIds()
	{
		List<String> privilegeIds = new ArrayList<String>();
		if (!getPrivileges().isEmpty())
		{
			for (Privilege p : privileges)
			{
				privilegeIds.add(String.valueOf(p.getId()));
			}
		}
		
		return privilegeIds;
	}
}
